package com.ch.clinking.fragment;
import static android.content.Context.MODE_PRIVATE;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.ListFragment;
import com.ch.clinking.ClinkingApplication;
import com.ch.clinking.R;
import com.ch.clinking.activity.AddMemberActivity;
import com.ch.clinking.activity.AutoCalculateMakeOrderActivity;
import com.ch.clinking.activity.CalculateMakeOrderActivity;
import com.ch.clinking.activity.CalculateProfitActivity;
import com.ch.clinking.activity.CalculateProfitListActivity;
import com.ch.clinking.activity.EditUserInfoActivity;
import com.ch.clinking.activity.LoginActivity;
import com.ch.clinking.bean.User;
import java.io.IOException;
import java.util.List;


public class MineFragment extends Fragment {

    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    private String mParam1;
    private String mParam2;
    private ImageView mineHeadPortrait;
    private TextView mineNickName, mineMerchantName, mineShopName, mineJobPosition;
    private Button mineLoginOutButton;
    private ViewGroup userProfileAllInfo;
    private RelativeLayout mineFunctionAddMemberAccount, mineFunctionCalculateProfitTool, mineFunctionCalculateMakeOrderTool, mineFunctionAutoMakeOrderCalculate, mineFunctionTodayProfit;
    private ClinkingApplication app;
    private List<User> users;

    public MineFragment() {
        // Required empty public constructor
    }


    public static MineFragment newInstance(String param1, String param2) {
        MineFragment fragment = new MineFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }

        app = ClinkingApplication.getInstance();
    }

    @Override
    public void onResume() {
        super.onResume();
        mineNickName.setText(app.user.getNickName());
        if (app.user.getShopId().equals("1")) {
            mineJobPosition.setText("管理员");
        }else {
            mineJobPosition.setText("成员");
        }

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_mine, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        userProfileAllInfo = view.findViewById(R.id.userProfileAllInfo);
        mineHeadPortrait = view.findViewById(R.id.mineHeadPortrait);
        mineNickName = view.findViewById(R.id.mineNickName);
        mineJobPosition = view.findViewById(R.id.mineJobPosition);
        mineMerchantName = view.findViewById(R.id.mineMerchantName);
        mineShopName = view.findViewById(R.id.mineShopName);
        mineLoginOutButton = view.findViewById(R.id.mineLoginOutButton);
        mineFunctionAddMemberAccount = view.findViewById(R.id.mineFunctionList1_1);
        mineFunctionCalculateProfitTool = view.findViewById(R.id.mineFunctionList1_2);
        mineFunctionCalculateMakeOrderTool = view.findViewById(R.id.mineFunctionList1_3);
        mineFunctionAutoMakeOrderCalculate = view.findViewById(R.id.mineFunctionList1_4);
        mineFunctionTodayProfit = view.findViewById(R.id.mineFunctionList1_5);

        initEvent();
    }

    private void initEvent() {


        userProfileAllInfo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent toEditeInfo = new Intent(MineFragment.this.getActivity(), EditUserInfoActivity.class);
                startActivity(toEditeInfo);
            }
        });

        mineLoginOutButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                // 获取 SharedPreferences 文件
                SharedPreferences preferences = requireContext().getSharedPreferences("user_prefs", Context.MODE_PRIVATE);
                SharedPreferences.Editor editor = preferences.edit();

                // 清除登录状态
                editor.putBoolean("is_logged_in", false);
                editor.apply();


                Intent toLoginIntent = new Intent(MineFragment.this.getActivity(), LoginActivity.class);
                startActivity(toLoginIntent);
                MineFragment.this.getActivity().finish();
            }
        });

        mineFunctionAddMemberAccount.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (app.user.getShopId().equals("1")) {
                    Intent toAddMember = new Intent(MineFragment.this.getActivity(), AddMemberActivity.class);
                    startActivity(toAddMember);
                } else {
                    return;
//                    Toast.makeText(getActivity(), "无权限", Toast.LENGTH_SHORT).show();
                }

            }
        });

        mineFunctionCalculateProfitTool.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent calculateProfit = new Intent(MineFragment.this.getActivity(), CalculateProfitActivity.class);
                startActivity(calculateProfit);
            }
        });

        mineFunctionCalculateMakeOrderTool.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent calculateProfit = new Intent(MineFragment.this.getActivity(), CalculateMakeOrderActivity.class);
                startActivity(calculateProfit);
            }
        });

        mineFunctionAutoMakeOrderCalculate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (app.user.getShopId().equals("1")) {
                    Intent autoCalculateMakeOrderActivity = new Intent(MineFragment.this.getActivity(), AutoCalculateMakeOrderActivity.class);
                    startActivity(autoCalculateMakeOrderActivity);
                }

            }
        });

        mineFunctionTodayProfit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                Intent calculateProfitListActivity = new Intent(MineFragment.this.getActivity(), CalculateProfitListActivity.class);
//                startActivity(calculateProfitListActivity);
            }
        });

    }
}